$(function () 
{
	//send the post request and hide and show everythign that needs it
	function getHide(name, url, options) {
		$("#" + name + "Error").text("");
		$("#" + name + "Loading").text("Submitting " + name +".");
		$.post(url, options , function(data) {
			$("#" + name + "Loading").text("");
			if (data == "success") {
				$("#" + name + "Error").text("");
				$("#" + name + "Over").data("overlay").close();
				window.location.reload();
			}
			else {
				$("#" + name + "Error").text(data);
			}
		}
		);
	}
	
	function getCourses() {
		//immediately retrieve the course information
		$.post("/getCourse", function(data) 
		{
			courseNames = eval(data); //store in courseNames                
			$(".coursesSelect").empty();
			//make sure that there are courses available to add into
			if (courseNames.length == 0) 
				$(".coursesSelect").append("<option value=index>No Courses Available</option>");
			else    
			{
				$(".coursesSelect").append('<option value=empty></option>');
				jQuery.each(courseNames, function(index, val) 
				{       		         		     		
					$(".coursesSelect").append('<option value=' + val + '>' + val + '</option>');        		      		
				});        	      
			};	                           
		});
	}
	getCourses();
    //if coursesSelect changes, then find the appropriate categories
	$('.coursesSelect').change(function() 
	{	
		$(".categoriesSelect").show();
		$(".categoriesSelect").children().remove();
		$(".categoriesSelect").append('<option value=loading>loading</option>');		
		//$(".categoriesSelect").append('<option value=loading>Loading...</option>');  		
  		//pass the selected course name to the post method and get the categories  		
  		/*
  		 * when we call the CLASS .coursesSelect, we're calling the createQuestion
  		 * createCategory, and eventually, the createClarification page. We need to either 
  		 * determine an order, or create separate functions for each one 
  		 */
  		$.post("/getCategory", {name: unescape($(this).find("option:selected").text())}, function(data) 
  		{
  			//ERROR in getting the right course value  					
 	 		try { 	 			
 	 			categoryNames = JSON.parse(data);	 
 	 			$(".categoriesSelect").children().remove();	 	 			
 	 			jQuery.each(categoryNames, function(index, val) 
	        	{	        				     	
  		      		$(".categoriesSelect").append('<option value=' + val + '>' + val + '</option>');
    	    	});
 	 		}catch(e) {
 	 			$(".categoriesSelect").children().remove();
				$(".categoriesSelect").append("<option value=loading>No categories available</option>");
 	 		}	 			  		
  		});
	}); 
	//if category changes
	$('.categoriesSelect').change(function()
	{	
		//alert("category: " + $(".categoriesSelect :selected").text());
	});
	//when the submit button is clicked, collect entries and send them to makeQuestion   
	$('#questSubmitButton').click(function()
	{	
		getHide("question", "/makeQuestion", {qName: $("#questName").val(), qText: $("#questText").val()
		, choiceA: $("#choiceA").val(), choiceB: $("#choiceB").val(), choiceC:$("#choiceC").val()
		, choiceD: $("#choiceD").val(), choiceE: $("#choiceE").val(), answer: $("#answerSelect").val()
		, course: $("#questCoursesSelect :selected").text(), category: $("#questCategoriesSelect :selected").text()});
	});
	
	//submit button for making a category
	$('#catSubmitButton').click(function()
	{	
		getHide("category", "/makeCategory", {name: $("#categoryName").val()
		, subtitle: $("#categorySubtitle").val(), 
		course: $('#categoryCoursesSelect option:selected').text()});
	});
	//submit button for making a course
	$('#courseSubmitButton').click(function()
	{
		getHide("course", "/makeCourse", {cName: $("#courseName").val()
		, cDescription: $("#courseDescription").val()});
	});
	//submit button for clarification
	$('#clarSubmitButton').click(function()
	{
		getHide("clarification", "/makeClarification", {cName: $("#clarificationName").val(), cText: $("#clarificationText").val()
		, course: $("#clarificationCoursesSelect :selected").text()});
	});
	//when the submit button is clicked, find the course and add it to their categories   
	$('#findSubmitButton').click(function()
	{
		getHide("find", "/findQuestion", {courseKey: $("#findBox").val().replace(/ /g,"")});
	});
	$('#ckSelect').change(function()
	{	
		$("#ckSelect").hide(); $("#CKeyCourse").text("Loading...");
		$.post("/createCKey", {course:  $("#ckSelect :selected").text()}, 
		function(data) { 
			if (data.length== 5) { 
				$("#CKeyCourse").text("Course \"" + $("#ckSelect :selected").text() 
				+ "\" key: " + data);
			}
			else {
				$("#CKeyCourse").text(data);
			}
			$("#ckSelect").show() });
	});
});